Bug fixes

Several bug fixes