Parameterized center-finding function, cleaned up code