Refactored java code, abstracted clicked() function