Updated code for CB 1.7.2.
May still have bugs.