Refactored and fixed bugs.