Refactoring + errors fixing