Reorganized the code