Separated out all the generic stuff into separate php files and added includes to them. Fixed javascript rendering race condition.