Refactor AutoPurge source and partially fix it