code cleanup & optimistic thread locking