This is a first attempt which can be improved. What is the purpose of the instanceof calls?