Patched fixes from issue 40 into issue 33 for easier merging.
Merge pull request #42 from integrii/issue-33

Patched fixes from issue 40 into issue 33 for easier merging.