The main change here is that we factorize all the interesting code in a single function. This should result in smaller code.