Separated wait() and signal() lines, added ending.