Refactoring some pieces of code