Fixed some server-side crashes and rewrote some code to make it more manageable