Added patch from Andrej Pavlovic embedded in blog