Prevent totalValue from being negative (fix #15)