Further fixes to analysis code (all tests now passing)