1. fix a bug in refinePath;
2. optimize a stupid bug in extractPath, which will lookup in a big
dictionary twice;
3. some minor refactoring