refactored for replace and deletion