final code for part 3 that works