Refactoring done for the most part