Refactored code to include tooltip function