refactor serInterval function