Revert "Revert "Multiple improvements and fixes""

This reverts commit fab6d241dc2a57e44762245cbba190b22ca6116f.