Brought in useful code from master