Implement rewriteTop function.