rewrite this without the `try-catch` for control flow. Also unit tests for all these cases?