improve threading and cleanup a bit