refactoring help from Stack Exchange