Fixes array segfault bug at expense of speed. Will return to optimize.