Improved the delete function to avoid second database call