fixing bugs exposed by #16 fix.
better logging and such for optimization