refactored javascript