Bug fixes, unit tests