Moved to dynamic arrays to avoid stack mem limits