Code like this is repeating every few lines, it should be a function