Moved some logic in the document on click function into it's own named function.