bug fixes and a state check algorithm for debug builds