refactored code a bit with consideration to x and y axis