Factor out a function from key loading