Fixed issues raised by @iguananaut and added a regression test