Fix for issue #312, #315, #317, #319 and #321