Fixed problems and refactored more