[core][main][error] Refactor main loop and error handling.