Refactored rename function