Refactor KSum and add some comments