Modifying the solution as per Rob's suggestions