Big changes following Wei's suggestions for #411 and #412