Modularized the code further