replaced lock with semaphore in order to allow cross thread usage