Fix logic error in balance function

The previous implementation gave uneven weighting to some values.
Merge pull request #7 from ryandesign/master

Fix balance function