Code seems to be doing what I want, but not giving right numbers