added debug mode and bugfixed errors