ported code phase 2, more stable