updated code with nested arrays