new version with more modularity in calculate