Moved key mod process into its own function for the reason defined in the previous commit message.