Improve structure and add optimizer.