Rearranged event handlers
Added documentation and comments