Code refactoring pass 2