Merge Alex's code

Has to be manually done because there are changes to stuff that could introduce errors.