Ticket/544

First version of fix for #544

Not yet read for merge, needs some more tests