There's loads of identical code that could be extracted. Write tests BEFORE extracting into functions.