Re-organized the code, Final Version