Various performance tweaks and code cleaning.