Added Ian Welch's updated code.
Bugfixes
Moved changes from test folder to correct place