worked on cleaning up the code and calculateQFunction calls