code modularized a bit