refactored rmdir implementation