make this logic more robust, I think