modularized some code