fixed array indexing bug

Cleaned up code formatting and fixed major array indexing bug (line 142)