Fixed the valid flag checker