Refactor code

Create new event handlers that calls just the functions that response to
the events. This makes the code much more testable.
Create new test units for those functions.
Switch from focusin to focus again.