Some performance or memory optimizations

Replaced String concat in loops with StringBuilder (performane)
Replaced for with keySet + Map.get() through for with entrySet (perf)
Changed inner classes to static classes, mostly struct like (memory)