refactored some javascript