code cleanup + tests