Dropping in old code, moving things into lib