Fixed output for 2_7 and refactored code to avoid duplication