This is pre-existing code: this branch already changes a lot of stuff, I tried to keep pre-existing logic as is, even when improvements were clearly possible.