Refactor and add tests for the three use cases in #3