Fix issue #56

Reverting commit b259ad9f3c7a4e718449a858a3b3302703f71d4b plus additional changes to fix issue #356