solve function works, fixed off-by-one error in all 3 functions