refeactored Javascript