code cleanup, fix unit tests