clean up repository implementation and fix some possible memory leaks