Fix bad `width`, `height` calculation + code cleanup/add comments