This should fix the array pointer issue.  Still need to add optimization.