Merging code from trunk that fixes #774, this work was done by incastrix