refactor position calculation