refactored js handling