Clean up the dirty loop

Needs testing for possible memory leaks
Merge pull request #252 from dreamteam69/master

Clean up the dirty loop