Fixed a serious bug with the code. Seems to be working well now.
Merge remote-tracking branch 'origin/master'