Patch provided by Adrian (slightly reworked by Constantin).