Major code cleanup and refactor, this fixes most of the codebase issues.