Added validation and deleted unnecessary code