Fixes a host of errors discoverd in testing