Rewrote JS in more readable format.