refactor and fix restart