fixes and some refactoring