Major changes and code cleanup