Working version with inverted indices