part ii optimization of code 9587577