@Yordan
I wonder if it might be related to the behaviour explained in this thread.
Some commands are not queued when they use a global variable as a parameter. It is likely the same when you use a local variable.
M42 is one such command.
Try putting M400 before your all of M42 and G53 calls.