Restructure server code to make embedding easier