refactoring, better error managment