Partially merged pull request #1 with modifications.