add debug flag and added extra if to make sure that array does not get to big