fixed a keypress handling bug. Some minor code cleanup.