Implement optimization functions.
Hasty commit.