Refactored and fixed a bug.