Various improvements in the code. Thanks loic84 :)