refactored code, added sweep loops