mem leak fix and add sum