Added refinements to error checking in various functions