Improved top rewriting, addressing #54