made error checking more efficient