Final PHP commit unless errors are found or stuff is added.
Code has been completely refactored and cleaned up.
Everything seems to be working.