Moved some code around to make PHP 5.3 happy