Fix minor issues and add tests for key handling

There are still issues with this code (starting with the name), but this is a start.

Tests are not built automatically, at least for now.
Just so it's on public record, I don't like this boolean trap.

I don't expect it to be fixed now, though.