preliminary merge with james' code (everything is broken right now)