Simplify logic
Fix output glyph array length computation

Based on patch from Dejan Kovacevic.