Reworked all debug code and general improvements.