some subtle bug fixes