Fixes for ticket #382, #337 and misc