Updating version with precomputation