Revert "some refactoring + bug fixes in key generation"

This reverts commit 8f42e28d998447fd4f6311dc1748572bb2c8cf11.
some refactoring + bug fixes in key generation (fixed bad refactoring in previous commit)