Moved code from the event handlers to new methods that the event handlers call