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.
Yeah. I'm not going to get distracted solving those problems now.