Updated version that supports errors