Lots of code... clean this up later.