refactor to fix restart bug