refactored js for readability