fixing array indexing bug