adding some optimization function to Chap 5