Refactor main and session