fixing casting and indexing bug