Fixed duplication bug
A better fix
Refactoring and fixed some bugs