more refactoring; working code