Refactor to decouple algorithm and render logic