Eliminate loop to match program specs, add tests