Moved the rest of the code to the library; added more tests