added additional check in error handling