implemented most of the mangle steps