Add part of PR #4 and fix issue #1