Add fit functionality, Refactor some code