Fixed format values method from hanging because of infinite loop.
Also cleaned up method, tested to ensure it works.