Fix #38, partial fix for #18 and fix for #19

See bugs for details