Enhancements

Rewritten code working with threads to get it working, some other issues fixed too.