better way to build arrays in loop