did some quality refactoring with the help of wbhumphrey