add exception handling, small refactor