logic separation, db  implemented and added locks, fixed ami