Factored out keychecking into a separate function

Made the code a little bit cleaner I think