Refactor position function