Extract some ugly conditionals to a helper func
Extract string length calculation to a func
Fix bug and remove ad hoc code that was masking it
Use ARR_LEN macro and remove dead code