Fixed array handling logic and refactored code