Cleaned up debug code, added comments for clarity