move code to use Q objects and generate javascript