Cleaned up server code, added some more logic.