add support for simple arrays at top level and complex arrays
little bit of clean up and optimization