Bugfix for issue 516

These commits fix issue #516