Add tests and fix completion corner cases

More tests to come, but this already fixes a couple of issues I found