Refactored javascript