Modulized the code a bit more