Finalized purging logic and added debug messages