Move again some code into a function
Simplify the code