Catch up with latest changes on master, minor refactoring.