Updated with Mark Kilgard's changes