Complete modularized working code