code clean up and macro implementation from