Refactor out duplicate code paths

Use StringBuilder instead of String.
Only touch dimensions once.

Change-Id: Idfe02df5c86874d6fcb1917b2186825f97de6bb6