Cleaned up code for fitting