Indexing fix, improved logic