bugfix + safety check in posToIndex.