working version with slight changes in finding summations