Refactoring, many fixes