Refactored timer loop