adding some context and moving the is_* tests farther up the stack