Refactored setTimeout method