refactored JS to be more concise