fixed warnings and errors, some refactoring