Enhancement: refactor path related code and add some tests