A bunch of fixes suggested by jrick