modified a logic and added debug functions.
Merge branch 'dev'