Code cleanup + unit tests