corrected indentation and refactor js