Updated Problem 6 with better solution