Minimal modifications to the code, bugs solved