refactored to provide blocking function rather than thread