refactored js slightly