cleanup and a bit of re-factoring