changes to js and controller logic. some refactoring