refactored js code