made error checking more logical