Several bug fixes