Cleaned up key handling code, moved IO to a seperate file