added a bunch of new changes and some refactoring