Added rm function and some refactoring