refactor: split up closure into two functions