Merge Develop into Master

This is a big rewrite of the api class file, please run a large test on all endpoints before you merge and check that there is no issues.

Logic has been cleaned up and some PSR2 styling applied throughout.