some refactoring, possible major fix