Macro cleanup, minor fixes

Mostly replaced codes with constants.
Minor code changes to better reflect implementation intent.