A lot of changes to make scipy minimize work.