fixed off-by-one errors, floating point errors, and added test cases