Further improvement to rate function; implement keypress handling