Refactored js to work on the server