Major refactoring and changing PHP to javascript