Refactoring some bit related to onclick listener and reformatting the code