Code cleanup and minor optimization