debugged latest code with macro management logic