Refactored tooltip position algorithm