Refactored a whole lot