moved db delete inside if, and added comments