Reverted to old version of wait() since the new one doesn't seem to work properly...