Big refactor to move some re-usable to out into a shared library