works as intended even though the code isn't very pretty