Fixing undefined offset bug