Moved all handling of the various modes into handler functions to aid in navigatability.