Refactored the javascript