Finished refactoring php/api.php
Refactored PHP a bit more and found a couple of typos. Refactored JavaScript