Changed to multidimensional array, added helper functions, added docstrings, improved performance