Refactoring and safety checking in the TBALogic file