Added validation to prevent forever loop