Merge pull request #1232 from alex/simplify2

Factor out a function from key loading