lots of changes mostly refactoring