Fixes #580, #581

Hi Patrick,

please merge this major bugfix into the master.
Please have a look at #580 and #581.

Cheers,
Ruben