Extract into main.php, reintroduce error handling