Cleaned up client-side code and added comments, will be easier to test now