Problems solved. It remains to test it more and to optimize it