Add defensive checks/asserts to all functions