made error checking more reusable