Reorganize error handling