added debug option and debugged