Merged code

Merged code with Patrick's latest work