Fixed building `keyPath` when the path is "/" root

Added additional unit test case and refactor function declarations