refactoring
adding errors handling