lots of changes and refactorings