moved some code
reorganized a bit the code