Modified to use macros for error checking to avoid scoping