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.
You could const_cast to keep the interface, but I guess it isn't important.