Refactored rwg significantly