Minor optimizations provided by @ryancramerdesign