refactor cleanup code