refactored logic for calculating x and y