fixed indexing to work correctly