refactor and add sqlalchemy dependency