Improve trend code