Code cleanup and adding debug statements