Fix #516
Fix #517
Fix #521
Fix #518