SERVER: bug fixes and some refactoring