join code update

This is based on Cheng's #221.