huge commit - lots of fixes and refactors