fixing cosmetic bugs and some refactoring