Better error handling in some functions, added BAD_OKAY/BAD_ERROR macros for easier reading