refactors client side, cleans up code and adds comments for clarity