refactored code to work with arrays