Conversion to library, untested. Closes #4