update and generalize code for fit. fit_to_row function.