Refactored and fixed minor bug