Fix bugs, refactoring.