rearrange a few things, and use promises

needs testing