significantly modularized the code