attachFunctionToLoop now has better error handling