Refactor the code with better variable names and less code duplication