refactoring and more code for Sum-Product