Reorginized the code