Refactoring and and fix some location bugs