Fixed undefined offset bug and removed unused variable