Refactor get_h(), and some possible fixes

Improved code reuse will likely lead to less errors in this code,
and the output of the program.

Specifically, factored out the weighted value calculations.