improved corner handling