refactored with testing