Fixed error with empty database
Small js refactoring