Would be great to have some tests in place so there's some confidence that this stays working. Would also likely improve the code structure.