Organize the idle detect code

reorganized the code