Seems you are doing the same steps for two data structures. May be extract to a private method and apply twice?