Currently there's a big if-else block with repetitive code. I'd suggest to extract those lines below the if-else block for better readability and maintenance.