Refactoring to attempt to simplify some things