Refactorize code