factoring out coordinates code