refactored, kept original intent and output