Formatted code based on DRY principle and PREP8 for better readability