Added DEBUG_LEVEL and a few code revamps