closes #494 Refactor how optimize works to fit the existing architecture.