Fix for #87, #168 and #172