Updated with minor fixes from John