It seems like this entire section should be a recursive call.