Correctly calculate end of tags (fixes #15)