Cleaned up the PHP and reduced the cycles needed to complete the function