fixed double loop bug
small refactoring
bit of code cleaning, reorganize