refactored some if/else statement to make flow a bit better