little refactor and some improvement in locks