almost done, just the optimization left