Move server functionality into different functions

As suggested by @Maltysen in #10