move some code into lib