Refactored Javascript