Fix assignment reorder

The second commit contains a test that keeps failing for me. The logic seems right, but I think there is something I am missing. The test is technically not required, but would be a good addition.