fixed empty database checks and clean up issues