add obj function to final, optimised version