factor out some functions and build some unit tests