Working version, probably final, very hacked together