Fix for #533, #521,#535, #528, #525, #527