refactored first pass