Added debug mode, fixed some errors