Still just a bunch of hacks... no pretty code... but it works a lot better now