slight refactoring to take out loop