the reason why you hit the side is that your lower collision box is to low, so it only works if you hit the side low enough.
if you still hit the side, print out both Z values and based of that subtract some of your player Z until you dont get stuck, but still dont fall through from the top