No modification to ex3.2
Imporved implementation of expand(), full handling of invalid input.