several bug fixes found from tests