cleaned up code a little bit with macros, not sure if it is still very readable.