Integrate category optimization code plus a few other general fixes