rb 2.99 compatibility, not much tested
Modified some style issues and fixed the state save
Removed unicode calls