I modularized Sadman's working code