refactored to work with arrays