general clean up, added debugging and error flags