debugged most of it