Fixed error checking code