Fix last row problem

Also add a couple of test patterns and perform some cleanup.