Fix dirty marking when tags are not ordered. Fixes #548