Added cleanup and a couple elements to make the code cleaner