some cleanup and a bit of refactorying.