refactor and fixed some assues.