modularized the code a bit more