Split out the util functions

Breaking up the code a bit to be more clean, more testable