this is not really the Sf way, I have a PR pending with a refactoring, you can take a look at it