lots of additions and refactorings