Fixed a bug I discovered and refactored code.