Refactored and fixed a few errors.