I think it would still be useful to move this code to class/static methods there; makes it a bit easier to be a public API and test it.