Refactoring + several fixes