Refactored code, array functions moved into new helper