added debug mode and fixed a small bug