Refactored some code in the click functions