rename paramters semantically
refactor out duplicate while loops