Refactoring, small bug fixed