Fixed bugs that cause server crash and complete refactor/reorganization of server-side code