Clean up library and fix bugs