some improvements, still refactoring