Refactors code to reduce complexity