Refactor server to move functions to closer to their call sites.