My code with Professor Toth's help