Multitude of bug fixes and refactorings