Adding validate function, improving code comments