Refactored code and fixed loading system