Refactorized the code

No new feature was really added, just a better organisation of the code.