Refactored flagshape.js for testability, but I think I actually made it worse... more work needed.