Merged my code with Piers' changes
Merged changes