Implemented database insertion, fixed race condition