refactor to not use exceptions