getting closer to a solution, now sorts some things but still replacing others unnecessarily