Refactored to support older version of PHP