fixed and refactored a lot of stuff