complete code with adopting to database