Fixing #53 by refactoring key reading code