this was annoyingly difficult to code up for three separate reasons and also I feel like I shouldn't have had to implement it in the first place