Refactoring, fixing errors