Refactored code and improved class instantiation