Minor optimizations, shifted small portions of code around.