Smarter adjustment of sizes

There are 2 changes of functionality here:

1. margins are not added on the edges of the screen anymore (but still added between the windows)
2. after exhausting 1 direction of making window larger another direction is allowed

other changes are just trying to make the code cleaner

p.s. inter-dependency of changes is not absolute, so if you feel that any part of this is problematic I can create another pull-request with less changes