some bugs
Big refactoring