reorganized the code.