Added input validity check.