Modified to better support threading, disabled by default