Unit tests + minor fixes and cleanup