Refactor code for better position computation