Refactor server init code