Improved debugging and code cleanup