Moved some functions to a library; Added some tests