very sloppy, but working code