Refactoring key generation

Closes #18