Updated to handle all sorts of input sizes or adjust dynamically.