added some extra conditional checks