Fixed undefined variable bug in  method