big refactored and fix bugs.