Simplifying $last and $diff logic