bug fixings and tests