refactored delete code