Refactored code and added JS counter