Fix the rm command and do some refactoring