Refactored almost everything