Committing a somewhat working version.  Cannot solve the 3/40 case, not sure why.