Re-organized the code