improved js code for reuse and added collapsible button for all debug out