Fixing sql and some erronous lines