Fixes for issue #43, #38, #31, #40