Cleaning up PHP and experimenting with error handling