Formatted code with http://beta.phpformatter.com/