fixed up and added unit tests