this is an enormous if-else; is there a way to refactor certain bits out to make this code more readable and have less duplication? (might not be the case, but worth a try).