Tests and fixes

Tests heavily borrowed from the work of @gummif