improve the error check function