Refactor exception and nil handling.