Refactor Code and fix bug.