reorganized the code