Updated to work with PHP5.4+