Added optimization code for HF optimization