Added optimization for exercise 6