Big refactoring, add exceptions, cbr, unit test