a bunch of improvements and refactorings