Refactored thw whole thing