implemented new replace method which has better error logging features