refactring; tests; fixes