Refactored a fair bit