Simplified code by adding arrays and reducing repetition