code as graded. Optimalization function has bugs