rewrite chaining logic