Originally done in Swift until I came up on a deadline and rewrote everything in JS