Refactored client-side code into server-side code
Fixed some typos
Messy client-side code cleaned up and refactored into server-side code where necessary