added debug mode and better comments, changed a bunch of stuff