fixed a bug I introduced during cleanup, added optimize switch