Manually applied changes from pull requests #45 and #50 on the original repo.