add state constants, minor level_detect optimization