refactored minorly