added a function to test whether file exists or not to avoid extra calculations