I am not able to follow your version of encode_tags() and I have no way to test it so I can’t verify it.  However, I don’t see anything setting a value to -100.  Are you sure your loop is working correctly?