Refactor timer function