Debugged the whole thing