Moved stuff into module in lib and added some tests.