cleanup + fix bugs + add test