massive refactoring and general bug fixes and betterments