Refactor. Fix bugs found with ZBS
v.3.0.5.1