some refactoring and small error fixes