Code cleanup and understanding