Added debug analysis mode
Bug fixes and clean up