Extract method to perform dynamic key assignment.
Refactor _expand function.