reqorked js part 3.1

cleaned up code and formatted