Debugged, but not pretty