Added validation code - both for input and output.