small bug fix and refactoring