Final working version of code necessary for Q2B