Clean up code

Refactor code into separate functions out of core/run-server.  Also adds
some documentation to their purpose & usage.