fixing some small logical bugs and missing instantiations.