Looks like first attempt works fine. Changed main() testcase for a more manual, easy-to-read version; seems fine.