More clever sorting to keep tags in place