refactored js a bit