Ticket/544

Fix for #544 and smaller improvements

Ready for review, but still needs more tests (however, no bugs known at this point).