Fixed a bug in #3, took a different approach in #4 but I'm sure there's a better way, started #5