Reorganize batch code and fix tests
Remove commented-out code
Refactor further
Refactor further