merged the reviewed code by Evan