generalising code for problems in 1 and 2D as well.