refactored, added some MACROs, not yet tested