added caching and tests to the key generator code