unit tests and small changes to top level functions