some refactoring, small bug fixes