Refactoring some code