Fixed server issues and refactored a lot