added support for negative indices in toIndex