Refactoring classes and fixing error