Refactor javascript

hope this helps