Finally got the arithmetic figured out and the code to work correctly.