move bulk of code into lib/