refactor code for timer