half way through refactoring for speed...