I suspect this file could be simplified a bit, but let's not block the PR for that. We can clean it up later. I think it all should function correctly.