Refactoring code fixing bugs when resizing