Refactor

organize the server side code
+1 to @EndangeredMassa's suggestion