refactored based off nick/andy's suggestions