Minor optimizations and removed an unnecessary cast.