this function is way too overengineered