Fix for 1150

Fixes the issues raised in #1150. Plus some more. Also includes the commits by @marcusoftnet.