refactored code by adding arrays