Added locking for gathering random bytes. Fixed a huge memory leak.