refactor: DRY up code for expanding nodes