(Tentatively) finished code for server-side