working code, but need refactoring