couple of super serious bug fixes with unit tests
minor docstring