Bug fixes and improvements

Bug fixes and improvements