Refactored some code and fixed some issues.