Merge git pull request 313 from bcyrill with some modifications