Refactoring + fixe some bugs.