Fix bad use of sub.apply and other lifecycle improvements (plus cosmetic changes in unit test)