Refactorizing some code