Move function into utils, add tests for it.