further refactor to eliminate nesting