Updated with Jonathan's fixes