move main code into /lib