the if/else logic could've been simplified without so much copy-paste