Added bounds checking where Craig noted.