Fix array indexing problems