Modified crf.py to include optimisation step