Refactor code

Rename handle functions
Move validation helper function
Optimize key comparison

Skip the common prefix when the keys are compared