Update code for modal resize with fixes

This pull request fixes a couple of bugs and improves code quality.

CC @anselmh @melros