Refactored 50% of code