Refactored the js