Merged and revised much code from master