Refactored js code