added some debug code.
bug fixed logic.
Merge branch 'dev'