Posting code here for posterity.