fixed bugs with earlier rewrites