Moved logic to be more server side. Added comments