Bug. Check for ```codes[-1] == CLOSEPOLY``` too.