fix purge bug, refactor