Code cleanup and small performance enhancement