First round of unit tests + bug fixes