working version for fitting algorithm