Refactored a lot of stuff