Fixed most of the implementation errors upserting, should switch to using a writable CTE