modularized the code nicely