Refactor to decouple interaction code from the algorithm