Rework a lot of the code to be more debuggable, mainly removing macros and converting to functions