refactor and fix bugs....