Some code clean-up, added some macros and fixed errors