Fixed and improved PHP error handling