Modularized the code a bit.