Fixed invalid variable
Fixed several bugs