Adding debugs and some simplification of conditional logic.