Restructured js to be more readable