Refactoring to organize code more logically