Writing a better routine to avoid overlappings.