Little adaptation of the code for better performance (not perfect yet)